//
//  UIButton+ImagePosition.m
//  CHSimpleUI
//
//  Created by CY on 2025/2/5.
//

#import "UIButton+ImagePosition.h"

#import "UIButton+ImagePosition.h"

@implementation UIButton (ImagePosition)

- (void)setImagePosition:(BUTTON_IMAGE_LOCAL_TYPE)local margin:(CGFloat)margin {
    CGFloat imageW = self.currentImage ? self.currentImage.size.width : 0;
    CGFloat imageH = self.currentImage ? self.currentImage.size.height : 0;

    CGFloat titleW = self.titleLabel ? self.titleLabel.intrinsicContentSize.width : 0;
    CGFloat titleH = self.titleLabel ? self.titleLabel.intrinsicContentSize.height : 0;

    if (local == BUTTON_IMAGE_LOCAL_TYPE_LEFT) {
        self.imageEdgeInsets = UIEdgeInsetsMake(0, -margin / 2, 0, margin / 2);
        self.titleEdgeInsets = UIEdgeInsetsMake(0, margin / 2, 0, -margin / 2);
    }
    else if (local == BUTTON_IMAGE_LOCAL_TYPE_RIGHT) {
        self.imageEdgeInsets = UIEdgeInsetsMake(0, titleW + margin / 2, 0, -titleW - margin / 2);
        self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageW - margin / 2, 0, imageW + margin / 2);
    }
    else if (local == BUTTON_IMAGE_LOCAL_TYPE_TOP) {
        self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, (imageH - titleH) / 2 + margin, -titleW);
        self.titleEdgeInsets = UIEdgeInsetsMake(imageH / 2 + titleH + margin, -imageW, 0, 0);
    }
    else if (local == BUTTON_IMAGE_LOCAL_TYPE_BOTTOM) {
        self.imageEdgeInsets = UIEdgeInsetsMake((imageH - titleH) / 2 + margin, 0, 0, -titleW);
        self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageW, imageH / 2 + titleH + margin, 0);
    }
}

@end
